package escritorio.Ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.ScrollPaneConstants;

import escritorio.conn.ConnErro;
import escritorio.conn.ConnectionFactory;
import escritorio.conn.CustaDao;
import escritorio.javaBeans.Custas;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class CustaFrm extends JFrame {

	private JPanel contentPane;
	private JTextField txtData;
	private JTextField txtValor;
	private JTextArea txtDescricao;
	private JComboBox cmbCustas;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					CustaFrm frame = new CustaFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CustaFrm() {
		setTitle("Cadastro custas");
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 613, 535);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JLabel lblProcesso = new JLabel("Processo :");
		lblProcesso.setBounds(12, 13, 60, 16);
		panel.add(lblProcesso);

		cmbCustas = new JComboBox();
		cmbCustas.setBounds(100, 10, 195, 22);
		fillcmbCusta();
		panel.add(cmbCustas);

		JLabel lblData = new JLabel("Data :");
		lblData.setBounds(12, 53, 42, 16);
		panel.add(lblData);

		txtData = new JTextField();
		txtData.setToolTipText("Data no formato dd/mm/aaaa");
		txtData.setBounds(100, 50, 116, 22);
		panel.add(txtData);
		txtData.setColumns(10);

		JLabel lblValor = new JLabel("Valor :");
		lblValor.setBounds(12, 94, 42, 16);
		panel.add(lblValor);

		txtValor = new JTextField();
		txtValor.setBounds(100, 91, 116, 22);
		panel.add(txtValor);
		txtValor.setColumns(10);

		JButton btnIncluir = new JButton("Incluir");
		btnIncluir.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				adicionaCustas();
			}
		});
		btnIncluir.setBounds(339, 13, 97, 25);
		panel.add(btnIncluir);

		JButton btnEditar = new JButton("Editar");
		btnEditar.setBounds(339, 48, 97, 25);
		panel.add(btnEditar);

		JButton btnConsultar = new JButton("Consultar");
		btnConsultar.setBounds(339, 82, 97, 25);
		panel.add(btnConsultar);

		JButton btnSair = new JButton("Sair");
		btnSair.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
		btnSair.setBounds(476, 48, 97, 25);
		panel.add(btnSair);

		JLabel lblDescrio = new JLabel("Descri\u00E7\u00E3o :");
		lblDescrio.setBounds(12, 137, 72, 16);
		panel.add(lblDescrio);

		JPanel panelDescricao = new JPanel();
		panelDescricao.setBounds(22, 166, 551, 301);
		panel.add(panelDescricao);
		panelDescricao.setLayout(null);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane
				.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane
				.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		scrollPane.setBounds(0, 0, 551, 301);
		panelDescricao.add(scrollPane);

		txtDescricao = new JTextArea();
		txtDescricao.setFont(new Font("Monospaced", Font.PLAIN, 18));
		scrollPane.setViewportView(txtDescricao);
	}
	
	private void fillcmbCusta() {
		String sql = "select nroProcesso from Processo";
		try {
			Connection connection = new ConnectionFactory().getConnection();
			PreparedStatement stmt = connection.prepareStatement(sql);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				String proc = rs.getString("nroProcesso");
				cmbCustas.addItem(proc);
			}
			rs.close();
			stmt.close();
		} catch (SQLException e) {
			throw new ConnErro(null, e);
		}
	}

	protected void adicionaCustas() {
		Custas custa = new Custas();
		CustaDao daoC = new CustaDao();
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat data = new SimpleDateFormat("dd/MM/yyyy");
		try {
			cal.setTime(data.parse(txtData.getText()));
			custa.setValor(Double.parseDouble(txtValor.getText()));
		} catch (ParseException e) {
			System.out.println("Erro no parse da data em CustaFrm");
			e.printStackTrace();
		}
		custa.setData(cal);
		custa.setDescricao(txtDescricao.getText());
		daoC.insereCustas(custa);
		
		System.out.println("Gravado!");
	
		
	}
}
